import {defineStore} from "pinia";
import {ref} from "vue";
import {UserDto} from "@/type/user";
import {loginApi} from "@/api/user";

export const useUserStore = defineStore('userStore', () => {
    const userInfo = ref<UserDto | null>(null)
    const token = ref('')
    const login = (username: string, password: string) => {
        return new Promise(resolve => {
            loginApi({
                username: username,
                password: password
            }).then(res => {
                if (res) {
                    userInfo.value = res.user
                    token.value = res.token
                    resolve(res)
                }
            })
        })
    }
    return {
        userInfo,
        token,
        login
    }
}, {
    persist: true
})
